home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
graphic
/
eedsrc24.zip
/
EEP24SRC.ZIP
/
IGRAPHEP.C
< prev
next >
Wrap
Text File
|
1992-07-29
|
18KB
|
553 lines
/*****************************************************************************
* General routines to handle the graphics. *
* *
* Written by: Gershon Elber Ver 0.1, Oct. 1989 *
* *
* Support: Interface for the virtual device driver. *
*****************************************************************************/
#define SUPPORT_GIF /* Dump it out as GIF file. */
#ifdef SUPPORT_GIF
#include "gif_lib.h"
#endif /* SUPPORT_GIF */
#include <stdio.h>
#include "virtrstr.h"
#include "program.h"
#include "igraph.h"
/* #define DEBUG Dump virtual screen to Hercules graphic device. */
#ifdef __MSDOS__
#include <io.h>
#include <fcntl.h>
#include <bios.h>
#include <dos.h>
#include <conio.h>
#endif /* __MSDOS__ */
#define EPSON_SIZE_X 1024
#define EPSON_SIZE_Y 640
#define EPSON_SIZE_MAX 1024 /* The maximum of the two above. */
#define EPSON_SIZE_X2 (EPSON_SIZE_X / 2)
#define EPSON_SIZE_Y2 (EPSON_SIZE_Y / 2)
/* The epson specific are defined here: */
#define EPSON_WIDTH 80 /* 80 char per line. */
#define EPSON_PIXEL_2_CHAR 8 /* 8 pixels per char, in REG_DENSITY. */
#define EPSON_ESC "\x1b" /* Actually regular escape char. */
#define EPSON_RESET "\x1b\x40" /* Reset the printer - ESC @. */
#define EPSON_VERTICAL_SPACE "\x1b\x41\x08" /* 8/72 vert. - ESC A 0x08. */
#define EPSON_REG_DENSITY "\x1b\x4b" /* 640 pixels per 7.5" - ESC K. */
#define EPSON_DUAL_DENSITY "\x1b\x4c" /* 1280 pixels per 7.5" - ESC L. */
static int GraphicMode = FALSE;
static void PutString(int DirectPrint, char *Str, int Len);
static void PutString2(int DirectPrint, char *Str, int Len);
#ifdef __MSDOS__
#ifdef DEBUG
static void DisplayBufferOnHercules(void);
static void HercGraphics(void);
static void HercPutPoint(int x, int y);
static void HercText(void);
#endif /* DEBUG */
#endif /* __MSDOS__ */
/****************************************************************************
* Routine to reset all the system to starting condition : *
****************************************************************************/
void IGInitGraph(void)
{
if (!VirtInit(EPSON_SIZE_X, EPSON_SIZE_Y)) {
fprintf(stderr, "Failed to allocate virtual device (no memory).\n");
MyExit(1);
}
GraphicMode = TRUE;
}
/****************************************************************************
* Routine to close and shutdown graphic mode : *
****************************************************************************/
void IGCloseGraph(void)
{
if (GraphicMode) {
VirtClose();
GraphicMode = FALSE;
}
}
/****************************************************************************
* Routine to dump the screen to the desired output device. *
****************************************************************************/
void IGDumpScreen(OutputDriverType OutputKind, int DirectPrint,
BooleanType DoubleDensity)
{
#ifdef SUPPORT_GIF
static GifColorType GifColorMap[] = { { 0, 0, 0 }, { 255, 255, 255 } };
GifFileType *GifFile;
#endif /* SUPPORT_GIF */
int i, j, k, Count;
unsigned int Shift;
char LineBuffers[8][EPSON_SIZE_Y / 8 + 1], OutputBuffer[EPSON_SIZE_MAX],
LinePrefixLen[2];
switch (OutputKind) {
case OUTPUT_EPSON:
if (DirectPrint != 0) break;
#ifdef __MSDOS__
/* Make sure stdout is output for binary output and buffer it. */
setmode(1, O_BINARY);
#endif /* __MSDOS__ */
break;
#ifdef SUPPORT_GIF
case OUTPUT_GIF:
if ((GifFile = EGifOpenFileHandle(1)) == NULL) {
fprintf(stderr, "Failed to open gif file.\n");
MyExit(-1);
}
break;
#endif /* SUPPORT_GIF */
case OUTPUT_RAW:
#ifdef __MSDOS__
/* Make sure stdout is output for binary output and buffer it. */
setmode(1, O_BINARY);
#endif /* __MSDOS__ */
break;
default:
fprintf(stderr, "Output kind is not supported, exit.\n");
MyExit(-1);
}
#ifdef __MSDOS__
#ifdef DEBUG
DisplayBufferOnHercules();
return;
#endif /* DEBUG */
#endif /* __MSDOS__ */
switch (OutputKind) {
case OUTPUT_EPSON:
fprintf(stderr, "Dumping line [%d]: ", EPSON_SIZE_X);
/* Reset printer, and make sure no space between adjacent lines: */
PutString(DirectPrint, EPSON_RESET, 2);
PutString(DirectPrint, EPSON_VERTICAL_SPACE, 3);
for (i = 0; i < EPSON_SIZE_X; i += 8) { /* Dump 8 lines at once: */
fprintf(stderr, "\b\b\b\b%-4d", i);
for (j = 0; j <8; j++)
VirtGetBlock(i + j, 0, i + j, EPSON_SIZE_Y, LineBuffers[j]);
Shift = 1;
Count = EPSON_SIZE_Y / 8;
for (j = 0; j < EPSON_SIZE_Y; j++) {
OutputBuffer[j] = 0;
for (k = 0; k < 8; k++) {
OutputBuffer[j] <<= 1;
if (LineBuffers[k][Count] & Shift) OutputBuffer[j]++;
}
if ((Shift <<= 1) == 256) {
Shift = 1;
Count--;
}
}
/* Find last position of non empty pixel byte: */
for (j = EPSON_SIZE_Y - 1; j >= 0; j--)
if (OutputBuffer[j] != 0) break;
if (j != 0 || OutputBuffer[0] != 0) {
j++;
if (DoubleDensity) {
PutString(DirectPrint, EPSON_DUAL_DENSITY, 2);
LinePrefixLen[0] = (j * 2) % 256;
LinePrefixLen[1] = (j * 2) / 256;
PutString(DirectPrint, LinePrefixLen, 2);
PutString2(DirectPrint, OutputBuffer, EPSON_SIZE_Y);
}
else {
PutString(DirectPrint, EPSON_REG_DENSITY, 2);
LinePrefixLen[0] = j % 256;
LinePrefixLen[1] = j / 256;
PutString(DirectPrint, LinePrefixLen, 2);
PutString(DirectPrint, OutputBuffer, EPSON_SIZE_Y);
}
}
PutString(DirectPrint, "\x0d\x0a", 2);
}
break;
case OUTPUT_RAW:
fprintf(stderr, "Dumping line [%d]: ", EPSON_SIZE_X);
for (i = 0; i < EPSON_SIZE_X; i++) {
fprintf(stderr, "\b\b\b\b%-4d", i);
VirtGetBlock(i, 0, i, EPSON_SIZE_Y, LineBuffers[0]);
for (j = 0; j < EPSON_SIZE_Y / 8; j++)
putchar(LineBuffers[0][j]);
}
break;
#ifdef SUPPORT_GIF
case OUTPUT_GIF:
fprintf(stderr, "Dumping line [%d]: ", EPSON_SIZE_Y);
if (EGifPutScreenDesc(GifFile, EPSON_SIZE_X, EPSON_SIZE_Y, 1, 0, 1,
GifColorMap) == GIF_ERROR ||
EGifPutImageDesc(GifFile, 0, 0, EPSON_SIZE_X, EPSON_SIZE_Y,
FALSE, 1, NULL) == GIF_ERROR) {
fprintf(stderr, "Failed to write to gif file.\n");
MyExit(-1);
}
for (i = 0; i < EPSON_SIZE_Y; i++) {
fprintf(stderr, "\b\b\b\b%-4d", i);
VirtGetBlock(0, i, EPSON_SIZE_X, i, LineBuffers[0]);
Shift = 1;
Count = EPSON_SIZE_X / 8;
for (j = EPSON_SIZE_X; j >= 0; j--) {
OutputBuffer[j] = (LineBuffers[0][Count] & Shift) != 0;
if ((Shift <<= 1) == 256) {
Shift = 1;
Count--;
}
}
if (EGifPutLine(GifFile, (GifPixelType *) OutputBuffer,
EPSON_SIZE_X) == GIF_ERROR) {
fprintf(stderr, "Failed to write to gif file.\n");
MyExit(-1);
}
}
if (EGifCloseFile(GifFile) == GIF_ERROR) {
fprintf(stderr, "Failed to close gif file.\n");
MyExit(-1);
}
break;
#endif /* SUPPORT_GIF */
}
}
/******************************************************************************
* Dumps the string of given length, to Prt. No char in Str has special *
* meaning, and even zero (NULL) chars are dumped. *
* If however DirectPrint is non zero, string is dumped to specifed lpt port. *
******************************************************************************/
static void PutString(int DirectPrint, char *Str, int Len)
{
int i;
if (DirectPrint != 0) {
#ifdef __MSDOS__
for (i = 0; i < Len; i++) biosprint(0, Str[i], DirectPrint - 1);
#else
fprintf(stderr, "Output kind is not supported, exit.\n");
MyExit(-1);
#endif /* __MSDOS__ */
}
else
fwrite(Str, Len, 1, stdout);
}
/******************************************************************************
* Dumps the string of given length, to Prt. No char in Str has special *
* meaning, and even zero (NULL) chars are dumped. Every char is dumped twice. *
* If however DirectPrint is